package guoanmaker.personal.business.service;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.personal.business.model.PersonalIpstatistics;
import guoanmaker.personal.business.model.PersonalUserstatistics;
import guoanmaker.personal.business.repository.PersonalIpstatisticsRepository;
import guoanmaker.personal.business.repository.PersonalUserstatisticsRepository;

@Service
public class PersonalUserstatisticsService implements PersonalUserstatisticsServiceInterface {

	@PersistenceContext
	private EntityManager manager;
	
	@Autowired
	private PersonalUserstatisticsRepository personalUserstatisticsRepository;
	
	@Autowired
	private PersonalIpstatisticsRepository personalIpstatisticsRepository;
	
	@Transactional
	public int saveUserstatistics(String userid, int type, String commdityid,String ipString) {
		if(commdityid!=null){
			List<PersonalUserstatistics> listcom=personalUserstatisticsRepository.selectUserstatisticsByCommdityid(type,commdityid);
			if(listcom==null||(listcom!=null&&listcom.size()==0)){
				PersonalUserstatistics personalUserstatistics=new PersonalUserstatistics();
				personalUserstatistics.setCommdityid(commdityid);
				personalUserstatistics.setCount(1);
				personalUserstatistics.setCreatedate(new Date());
				personalUserstatistics.setType(type);
				personalUserstatisticsRepository.save(personalUserstatistics);
			}
			else{
				String jpql="update PersonalUserstatistics p set p.count=p.count+1 where p.commdityid=:commdityid and p.type=:type and "
						+ "date_format(p.createdate,'%y-%m-%d')=date_format(now(),'%y-%m-%d')";
				Query query=manager.createQuery(jpql);
				query.setParameter("commdityid", commdityid);
				query.setParameter("type", type);
				query.executeUpdate();
			}
		}
		List<PersonalIpstatistics> listip=personalIpstatisticsRepository.selectIpstatisticsByIp(ipString,type);
		if(listip==null||(listip!=null&&listip.size()==0)){
			PersonalIpstatistics personalIpstatistics=new PersonalIpstatistics();
			personalIpstatistics.setIp(ipString);
			personalIpstatistics.setDatetime(new Date());
			personalIpstatistics.setType(type);
			personalIpstatisticsRepository.save(personalIpstatistics);
		}
		if(userid==null){
			return -1;
		}
		if(type!=6||type!=7){
			List<PersonalUserstatistics> list=personalUserstatisticsRepository.selectUserstatisticsByUserid(userid);
			if(list==null||(list!=null&&list.size()==0)){
				PersonalUserstatistics personalUserstatistics=new PersonalUserstatistics();
				if(type==4||type==5){
					personalUserstatistics.setType(type);
				}
				personalUserstatistics.setUserid(userid);
				personalUserstatistics.setCreatedate(new Date());
				personalUserstatistics.setCount(1);
				personalUserstatisticsRepository.save(personalUserstatistics);
			}
			else{
				String jpql="update PersonalUserstatistics p set p.count=p.count+1 where p.userid=:userid and "
						+ "date_format(p.createdate,'%y-%m-%d')=date_format(now(),'%y-%m-%d')";
				Query query=manager.createQuery(jpql);
				query.setParameter("userid", userid);
				query.executeUpdate();
			}
		}
		if(type==1||type==2||type==3||type==6||type==7||type==8||type==9){
			List<PersonalUserstatistics> lists=personalUserstatisticsRepository.selectUserstatisticsByType(type);
			if(lists==null||(lists!=null&&lists.size()==0)){
				PersonalUserstatistics personalUserstatistics=new PersonalUserstatistics();
				personalUserstatistics.setType(type);
				personalUserstatistics.setCreatedate(new Date());
				personalUserstatistics.setCount(1);
				personalUserstatisticsRepository.save(personalUserstatistics);
			}
			else{
				String jpql="update PersonalUserstatistics p set p.count=p.count+1 where p.type=:type and "
						+ "date_format(p.createdate,'%y-%m-%d')=date_format(now(),'%y-%m-%d')";
				Query query=manager.createQuery(jpql);
				query.setParameter("type", type);
				query.executeUpdate();
			}
		}
		return 1;
	}

}
